<?php

require FCPATH . '/application/controllers/backend/backend.php';

class Contact extends Backend {

    public function __construct() {
	parent::__construct();
	$this->load->model('contact_model', 'model');
    }

    public function index() {
	$this->load_js('backend_contact');
	$this->load_css('backend_contact_index');
	$this->rend('contact');
    }
    
    public function show(){
	$id = intval($this->uri->segment(4));
	
	$record = $this->model->find($id);
	
	if(!$record){
	    die('404');
	}
	
	$this->rend('contact_show',array(
	    'record' => $record
	));
    }

    public function records() {
	$page = intval($this->input->get('page'));
	$page_size = intval($this->input->get('pageSize'));
	$page = $page ? $page : 1;
	$page_size = $page_size ? $page_size : 10;
	$offset = $page_size * ($page - 1);

	$conditions = array();
	$id = intval($this->input->get('id'));
	if (!empty($id)) {
	    $conditions[] = array('where', 'id', $id);
	}

	$title = trim($this->input->get('title'));
	if (!empty($title)) {
	    $conditions[] = array('or_like', 'title_zh', $title);
	    $conditions[] = array('or_like', 'title_fr', $title);
	    $conditions[] = array('or_like', 'title_en', $title);
	}

	$start = $this->input->get('start');
	if (!empty($start)) {
	    $conditions[] = array('where', 'created_at >=', $start);
	}

	$end = $this->input->get('end');
	if (!empty($end)) {
	    $conditions[] = array('where', 'created_at <=', $end);
	}

	$this->json_back('XT001', '', array(
	    'total' => $this->model->total($conditions),
	    'records' => $this->model->records(array(), $conditions, array(), $page_size, $offset)
	));
    }

    public function add() {
	$this->load_js(array(
	    'jquery-ui-1.8.20.custom.min',
	    'ImageBrowser',
	    'backend_contact_form'
	));

	$this->load_css(array(
	    'custom-theme/jquery-ui-1.8.20.custom',
	    'backend_contact_form'
	));
	
	$this->rend('contact_form');
    }

    public function edit() {
	$id = intval($this->uri->segment(4));
	$this->load_js(array(
	    'jquery-ui-1.8.20.custom.min',
	    'ImageBrowser',
	    'backend_contact_form'
	));
	
	$this->load_css(array(
	    'custom-theme/jquery-ui-1.8.20.custom',
	    'backend_contact_form'
	));

	$this->rend('contact_form', array('record' => $this->model->find($id)));
    }

    public function insert() {
	$data = array(
	    'map' => intval($this->input->post('map')),
	    'title_zh' => trim($this->input->post('title_zh')),
	    'title_fr' => trim($this->input->post('title_fr')),
	    'title_en' => trim($this->input->post('title_en')),
	    'address_zh' => $this->input->post('address_zh'),
	    'address_fr' => $this->input->post('address_fr'),
	    'address_en' => $this->input->post('address_en'),
	    'tel' => $this->input->post('tel'),
	    'fax' => $this->input->post('fax'),
	    'qq' => $this->input->post('qq'),
	    'email' => $this->input->post('email')
	);
	$this->validate($data);
	if (($id = $this->model->insert($data)) > 0) {
	    $this->json_back('XT001', '添加成功', array('id' => $id));
	}
	$this->json_back('XT002', '数据库或SQL语句有误。');
    }

    public function update() {
	$id = intval($this->input->post('id'));
	$data = array(
	    'map' => intval($this->input->post('map')),
	    'title_zh' => trim($this->input->post('title_zh')),
	    'title_fr' => trim($this->input->post('title_fr')),
	    'title_en' => trim($this->input->post('title_en')),
	    'address_zh' => $this->input->post('address_zh'),
	    'address_fr' => $this->input->post('address_fr'),
	    'address_en' => $this->input->post('address_en'),
	    'tel' => $this->input->post('tel'),
	    'fax' => $this->input->post('fax'),
	    'qq' => $this->input->post('qq'),
	    'email' => $this->input->post('email')
	);
	$this->validate($data);
	if ($this->model->update($id, $data)) {
	    $this->json_back('XT001', '更新成功', array('id' => $id));
	}
	$this->json_back('XT002', '数据库或SQL语句有误。');
    }

    public function delete() {
	$id = intval($this->input->get('id'));
	if ($this->model->delete($id)) {
	    $this->json_back('XT001', '删除成功。');
	}
	$this->json_back('XT002', '数据库或SQL语句有误。');
    }

    public function validate($data) {
	if (empty($data['title_zh'])) {
	    $this->json_back('XT002', '请填写标题（中文）');
	}
	if (empty($data['title_fr'])) {
	    $this->json_back('XT002', '请填写标题（法文）');
	}
	if (empty($data['title_en'])) {
	    $this->json_back('XT002', '请填写标题（英文）');
	}
	if (empty($data['address_zh'])) {
	    $this->json_back('XT002', '请填写地址（中文）');
	}
	if (empty($data['address_fr'])) {
	    $this->json_back('XT002', '请填写地址（法文）');
	}
	if (empty($data['address_en'])) {
	    $this->json_back('XT002', '请填写地址（英文）');
	}
    }

}
